package link.gpt.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import link.gpt.dao.GoodsDetailDao;
import link.gpt.pojo.GoodsDetail;
import link.gpt.service.GoodsDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @version V1.0A
 * @uthor gpt
 * @Project holiday_task
 * @Package link.gpt.service.impl
 * @Description: ${TODO}
 * @Date 2021/10/5 17:08
 */
@Service
public class GoodsDetailServiceImpl implements GoodsDetailService {

   @Autowired
   private GoodsDetailDao goodsDetailDao;

   @Override
   public GoodsDetail findById(Integer id) {
      return goodsDetailDao.findById(id);
   }

   @Override
   public int buyGoods(Integer id, int num) {
      GoodsDetail goodsDetail = goodsDetailDao.selectByPrimaryKey(id);
      if (goodsDetail == null) {
         throw new RuntimeException("购买商品不存在!");
      }

      if (goodsDetail.getRemaining() < num) {
         throw new RuntimeException("购买商品数量不足!");
      }

      return goodsDetailDao.buyGoods(id, num);
   }

   @Override
   public PageInfo<GoodsDetail> findAllBySortIdAndPage(Integer sortId, int num, int size) {
      PageHelper.startPage(num, size);
      List<GoodsDetail> goodsDetailList = goodsDetailDao.findAllBySortIdAndPage(sortId);
      PageInfo<GoodsDetail> goodsDetailPageInfo = new PageInfo<>(goodsDetailList);
      return goodsDetailPageInfo;
   }
}
